package com.banzhi.shop.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.data.GroupsBean;
import com.banzhi.shop.utils.ASUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.banzhi.shop.MyApp.context;
//拼团精选适配器

/**
 * Created by Administrator on 2018/7/16.
 */

public class GroupingAdapter extends RecyclerView.Adapter<GroupingAdapter.ViewHolder> {
    private List<GroupsBean> mList;
    private OnItemClickListener mOnItemClickListener = null;


    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View myView;
        ImageView imageView;
        TextView title;
        TextView groupsPrice;
        TextView Groupnum;
        TextView profit;
        TextView stock;
        ImageView ivGoGrouping;
      //  ImageView mIvEnd;
       // ImageView mIvGoEnd;
        TextView mTvFreight;
        TextView mTvYoufei;
        TextView mTvReminbi;
        private final ImageView mIvBaoyou;
        TextView TvGroupPro;
        public ViewHolder(View itemView) {
            super(itemView);
            myView = itemView;
            imageView = (ImageView) itemView.findViewById(R.id.iv_thumb);
            title = (TextView) itemView.findViewById(R.id.tv_title_grouping);
            groupsPrice = (TextView) itemView.findViewById(R.id.tv_groupsprice);
            profit = (TextView) itemView.findViewById(R.id.tv_profit_grouping);
            Groupnum = (TextView) itemView.findViewById(R.id.tv_num_grouping);
            stock = (TextView) itemView.findViewById(R.id.tv_buyed_num);
            ivGoGrouping = (ImageView) itemView.findViewById(R.id.tv_go_grouping);
        //    mIvEnd = (ImageView) itemView.findViewById(R.id.iv_end);
         //   mIvGoEnd = (ImageView) itemView.findViewById(R.id.iv_go_end);
            mTvFreight = (TextView) itemView.findViewById(R.id.tv_freight);
            mTvYoufei = (TextView) itemView.findViewById(R.id.tv_youfei);
            mTvReminbi = (TextView) itemView.findViewById(R.id.tv_reminbi);
            TvGroupPro = (TextView) itemView.findViewById(R.id.tv_group_pro);
            mIvBaoyou = (ImageView) itemView.findViewById(R.id.iv_baoyou);

        }
    }

    public GroupingAdapter(List<GroupsBean> list) {
        this.mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylerview_item_grouping_select, null);
        final ViewHolder holder = new ViewHolder(view);
        //条目点击控件
        holder.myView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                GroupsBean groupsBean = mList.get(position);
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view, (int) view.getTag());
                }

            }
        });
  /*      //子条目点击控件
        holder.ivGoGrouping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                GroupsBean groupsBean = mList.get(position);
              //  Intent intent = new Intent(this, GroupingDetailsActivity.class);
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view,(int)view.getTag());
                }
                ToastUtils.makeToastShort("点击了"+groupsBean.getId());

            }
        });*/

        return holder;
    }

    //将数据绑定到控件上
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        GroupsBean bean = mList.get(position);
        // holder.imageView.setBackgroundResource(bean.itemImage);
        holder.title.setText(bean.getTitle());
        holder.groupsPrice.setText(bean.getGroupsprice());//拼团价格
        holder.profit.setText(bean.getProfit());
        holder.TvGroupPro.setText(bean.getProfit());
        holder.Groupnum.setText(bean.getGroupnum());//拼团人数
        holder.stock.setText(bean.getSales());//库存
        if ("0.00".equals(bean.getFreight())/*||"0".equals(bean.getFreight())*/) {
            holder.mTvFreight.setVisibility(View.GONE);
            holder.mTvYoufei.setVisibility(View.GONE);
            holder.mTvReminbi.setVisibility(View.GONE);
            holder.mIvBaoyou.setVisibility(View.VISIBLE);

        }else {
            holder.mTvFreight.setText(bean.getFreight());
        }

        Long Now = System.currentTimeMillis() / 1000;

        if (Now > bean.getEndstr()) {
            if (ASUtils.isCanUse(bean.getThumb())) {
                Picasso.with(context).load(bean.getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.imageView);
            } else {
                Picasso.with(context).load(R.mipmap.zhanweitu2).into(holder.imageView);
            }
        //    holder.mIvEnd.setVisibility(View.VISIBLE);
        //    holder.mIvGoEnd.setVisibility(View.VISIBLE);
            holder.ivGoGrouping.setVisibility(View.VISIBLE);
        }else {
            if (ASUtils.isCanUse(bean.getThumb())) {
                Picasso.with(context).load(bean.getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.imageView);
            } else {
                Picasso.with(context).load(R.mipmap.zhanweitu2).into(holder.imageView);
            }
       //     holder.mIvEnd.setVisibility(View.GONE);
        //    holder.mIvGoEnd.setVisibility(View.GONE);
            holder.ivGoGrouping.setVisibility(View.VISIBLE);

        }

      /*  } else {
            if (ASUtils.isCanUse(bean.getThumb())) {
                Picasso.with(context).load(bean.getThumb()).placeholder(R.mipmap.zhanweitu2).into(holder.imageView);
            } else {
                holder.imageView.setImageResource(R.mipmap.zhanweitu2);
            }
            holder.mIvEnd.setVisibility(View.VISIBLE);
            holder.mIvGoEnd.setVisibility(View.VISIBLE);
            holder.ivGoGrouping.setVisibility(View.GONE);

        }*/
 /*       if (Now > bean.getEndstr()) {
            if (ASUtils.isCanUse(bean.getThumb())) {
                Picasso.with(context).load(bean.getThumb())*//*.placeholder(R.mipmap.zhanweitu2)*//*.into(holder.imageView);
            }*//*else {
               // holder.imageView.setImageResource(R.mipmap.zhanweitu2);
                Picasso.with(context).load(R.mipmap.zhanweitu2).into(holder.imageView);
            }*//*

            holder.mIvEnd.setVisibility(View.VISIBLE);
            holder. mIvGoEnd.setVisibility(View.VISIBLE);
            holder.ivGoGrouping.setVisibility(View.GONE);
        }*/

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    //下面两个方法提供给页面刷新和加载时调用
    public void add(List<GroupsBean> addMessageList) {
        //增加数据
        int position = mList.size();
        mList.addAll(position, addMessageList);
        notifyItemInserted(position);
    }

    public void refresh(List<GroupsBean> newList) {
        //刷新数据
        mList.removeAll(mList);
        mList.addAll(newList);
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
